package com.ganyu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganyu.domain.ResponseResult;

import com.ganyu.domain.dto.AdminUserRoleDto;
import com.ganyu.domain.entity.Role;
import com.ganyu.domain.entity.User;
import com.ganyu.domain.vo.*;

import com.ganyu.exception.SystemException;
import com.ganyu.mapper.RoleMapper;
import com.ganyu.mapper.UserMapper;
import com.ganyu.mapper.UserRoleMapper;
import com.ganyu.service.AdminUserService;

import com.ganyu.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.ganyu.enums.AppHttpCodeEnum.*;

/**
 * DATA: 2022/10/2
 * Author: 晨夕
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<UserMapper, User> implements AdminUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;
    //分页查询用户
    @Override
    public ResponseResult selectUserList(Integer pageNum, Integer pageSize, String userName, String phonenumber,String status) {
        List<AdminPeopleInfoVo> adminPeopleInfoVos = userMapper.selectUserList((pageNum - 1) * pageSize, pageSize, userName, phonenumber,status);
        int i = userMapper.selectUserCount(userName, phonenumber,status);
        PageVo<AdminPeopleInfoVo> pageVo = new PageVo<>();
        pageVo.setRows(adminPeopleInfoVos);
        pageVo.setTotal(i);
        return ResponseResult.okResult(pageVo);
    }


    //添加用户
    @Override
    public ResponseResult insertUser(AdminUserRoleDto adminUserRoleDto) {
        //1. 用户名不能为空
        if(ObjectUtils.isEmpty(adminUserRoleDto.getUserName())){
            throw new SystemException(REQUIRE_USERNAME);
        }
        //2. 用户名不能重复
        if(!ObjectUtils.isEmpty(userMapper.selectByUserName(adminUserRoleDto.getUserName()))){
            throw new SystemException(USERNAME_EXIST);
        }
        //3. 手机号不能重复
        if(!ObjectUtils.isEmpty(userMapper.selectByPhone(adminUserRoleDto.getPhonenumber()))){
            throw new SystemException(PHONENUMBER_EXIST);
        }
        //4. 邮箱不能重复
        if(!ObjectUtils.isEmpty(userMapper.selectByEmail(adminUserRoleDto.getEmail()))){
            throw new SystemException(EMAIL_EXIST);
        }
        //对密码进行加密操作
        String encode = passwordEncoder.encode(adminUserRoleDto.getPassword());
        adminUserRoleDto.setPassword(encode);
        userMapper.insertUser(adminUserRoleDto);

        //获取的roleIds添加到数据库中
        Long aLong = userMapper.selectIdByUserName(adminUserRoleDto.getUserName());
        //删除原来的
        userRoleMapper.deleteRoleByUserId(aLong);
        //获取roleIds，并插入
        List<Long> roleIds = adminUserRoleDto.getRoleIds();
        for(Long roleId : roleIds){
            userRoleMapper.insertRole(aLong,roleId);
        }
        return ResponseResult.okResult();
    }

    //删除用户
    @Override
    public ResponseResult deleteUser(Long[] ids) {
        userMapper.deleteUser(ids);
        //删除user_role表中的
        for(Long id : ids){
            userRoleMapper.deleteRoleByUserId(id);
        }
        return ResponseResult.okResult();
    }

    //回显用户数据
    @Override
    public ResponseResult selectUser(Long id) {

        //查出所有的role
        List<Role> allRole = roleMapper.getAllRole();
        List<AdminPeopleInfoVo> adminPeopleInfoVos = BeanCopyUtils.copyBeanList(allRole, AdminPeopleInfoVo.class);
        AdminUpdateUserVo adminUpdateUserVo = new AdminUpdateUserVo();
        adminUpdateUserVo.setRoles(adminPeopleInfoVos);
        //获取用户信息
        User user = userMapper.selectUserById(id);
        AdminUserSelectVo adminUserSelectVo = BeanCopyUtils.copyBean(user, AdminUserSelectVo.class);
        adminUpdateUserVo.setUser(adminUserSelectVo);
        //获取用户的role
        List<Long> roleIdList1 = userRoleMapper.getRoleIdList(id);
        adminUpdateUserVo.setRoleIds(roleIdList1);
        return ResponseResult.okResult(adminUpdateUserVo);
    }

    @Override
    public ResponseResult updateUser(AdminInsertUserVo adminInsertUserVo) {
        userMapper.updateUser(adminInsertUserVo);
        //删除原来的
        userRoleMapper.deleteRoleByUserId(adminInsertUserVo.getId());
        //获取roleIds，并插入
        List<Long> roleIds = adminInsertUserVo.getRoleIds();
        for(Long roleId : roleIds){
            userRoleMapper.insertRole(adminInsertUserVo.getId(),roleId);
        }
        return ResponseResult.okResult();
    }
}
